package com.yunzo.coc.common.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.yunzo.coc.common.bean.coc.CotactsNode;

public class NodeUtil {
	private List<String> returnList = new ArrayList<String>();
    
    /**
     * 根据父节点的ID获取所有子节点
     * @param list 分类表
     * @param typeId 传入的父节点ID
     * @return String
     */
    public String getChildCotactsNodes(List<CotactsNode> list, String typeId) {
        if(list == null && typeId == null) return "";
        for (Iterator<CotactsNode> iterator = list.iterator(); iterator.hasNext();) {
        	CotactsNode cn = (CotactsNode) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (typeId==cn.getIds()) {
                recursionFn(list, cn);
            }
            // 二、遍历所有的父节点下的所有子节点
            /*if (CotactsNode.getParentId()==0) {
                recursionFn(list, CotactsNode);
            }*/
        }
        return returnList.toString();
    }
     
    private void recursionFn(List<CotactsNode> list, CotactsNode cn) {
        List<CotactsNode> childList = getChildList(list, cn);// 得到子节点列表
        if (hasChild(list, cn)) {// 判断是否有子节点
            returnList.add(cn.getIds());
            Iterator<CotactsNode> it = childList.iterator();
            while (it.hasNext()) {
                CotactsNode n = (CotactsNode) it.next();
                recursionFn(list, n);
            }
        } else {
            returnList.add(cn.getIds());
        }
    }
     
    // 得到子节点列表
    private List<CotactsNode> getChildList(List<CotactsNode> list, CotactsNode cn) {
        List<CotactsNode> cotactsNodeList = new ArrayList<CotactsNode>();
        Iterator<CotactsNode> it = list.iterator();
        while (it.hasNext()) {
        	CotactsNode n = (CotactsNode) it.next();
            if (n.getParentids().equals(cn.getIds())) {
            	cotactsNodeList.add(n);
            }
        }
        return cotactsNodeList;
    }
 
    // 判断是否有子节点
    private boolean hasChild(List<CotactsNode> list, CotactsNode cn) {
        return getChildList(list, cn).size() > 0 ? true : false;
    }
     
     
    // 本地模拟数据测试
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        List<CotactsNode> clt = new ArrayList<CotactsNode>();
        CotactsNode CotactsNode1 = new CotactsNode("1", "蔬菜", "");
        CotactsNode CotactsNode2 = new CotactsNode("2", "水产", "1");
        CotactsNode CotactsNode3 = new CotactsNode("3", "畜牧", "1");
        CotactsNode CotactsNode4 = new CotactsNode("4", "瓜类", "2");
        CotactsNode CotactsNode5 = new CotactsNode("5", "叶类", "3");
        CotactsNode CotactsNode6 = new CotactsNode("6", "丝瓜", "3");
        CotactsNode CotactsNode7 = new CotactsNode("7", "黄瓜", "4");
        CotactsNode CotactsNode8 = new CotactsNode("8", "白菜", "4");
        CotactsNode CotactsNode9 = new CotactsNode("9", "虾", "4");
        CotactsNode CotactsNode10 = new CotactsNode("10", "鱼", "6");
        CotactsNode CotactsNode11 = new CotactsNode("11", "牛", "8");
         
        clt.add(CotactsNode1);
        clt.add(CotactsNode2);
        clt.add(CotactsNode3);
        clt.add(CotactsNode4);
        clt.add(CotactsNode5);
        clt.add(CotactsNode6);
        clt.add(CotactsNode7);
        clt.add(CotactsNode8);
        clt.add(CotactsNode9);
        clt.add(CotactsNode10);
        clt.add(CotactsNode11);
         
        NodeUtil mt = new NodeUtil();
        System.out.println(mt.getChildCotactsNodes(clt, "1"));
        long end = System.currentTimeMillis();
        System.out.println("用时:" + (end - start) + "ms");
    }
     
}

